package com.angular.useradmin.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.angular.useradmin.user.common.result.R;
import com.angular.useradmin.user.entity.Message;
import com.angular.useradmin.user.mapper.MessageMapper;
import com.angular.useradmin.user.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 许根银
 * @since 2023-01-04
 */
@RestController
@RequestMapping("/user")
@Api(description = "user")
@CrossOrigin
public class MessageController {
    private final MessageService messageService;

    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    @GetMapping("/getall")
    @ApiOperation("test")
    public R save() {
        List<Message> list = messageService.list(null);
        return R.ok().data("list",list);
    }

    @PostMapping("/save")
    @ApiOperation("新增接口")
    public R save(@RequestBody Message message) {
        boolean save = messageService.save(message);
        return R.ok();
    }

    @GetMapping("/getbyid/{id}")
    @ApiOperation("通过id获取用户详细")
    public R getById(@PathVariable String id) {
        Message message = messageService.getById(id);
        return R.ok().data("message", message);
    }

    @PutMapping("/edit")
    @ApiOperation("/根据用户id修改信息")
    public R edit(@RequestBody Message message) {
        boolean b = messageService.updateById(message);
        return R.ok();
    }

    @ApiOperation(value = "分页操作")
    @GetMapping("/page/{current}/{limit}")
    public R pageList(@PathVariable long current, @PathVariable long limit) {
        Page<Message> page = new Page<>(current, limit);
        messageService.page(page, null);
        long total = page.getTotal();//总记录数
        List<Message> records = page.getRecords();//数据list集合
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

}

